package reflect.ReflectDemo1;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asd73 on 2017/8/21.
 */
 class Demo {
    /**
     * 反射构造函数Person()
     * @param args
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class  clazz = Class.forName("reflect.ReflectDemo1.Person");
        Constructor constructor = clazz.getConstructor(null);
        Person p = (Person) constructor.newInstance(null);
        System.out.println(p.name);
    }
}
class Demo1{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Constructor constructor = clazz.getConstructor(String.class);
        Person p = (Person) constructor.newInstance("name");
        System.out.println(p.name);
    }


}

class Demo2{
    /**
     * 测试类内部的私有类是不能被外部类调用的
     * 除了在反射的时候可以暴力反射
     * @param args
     */
    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Constructor constructor = clazz.getDeclaredConstructor(List.class);
        constructor.setAccessible(true);//暴力反射
        Person person = (Person) constructor.newInstance(new ArrayList<>());
        System.out.println(person.name);
    }

}
class Demo3{
    /**
     * 反射类的方法public void method1()
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Method method = clazz.getMethod("method1",null);
        method.invoke(p,null);
    }
}

class Demo4{
    /**
     * 反射类的方法public void method1()
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Method method = clazz.getMethod("method1",String.class,int.class);
        method.invoke(p,"wchen",27);
    }
}
class Demo5{
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Method method = clazz.getDeclaredMethod("method1", InputStream.class);
        method.setAccessible(true);
        method.invoke(p,new FileInputStream("d:\\虚拟机账号密码.txt"));

    }
}
class Demo6{
     //反射类的方法 public static void method1(int num)
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Method method = clazz.getMethod("method1",int.class);
        method.invoke(null,23);
    }
}
class Demo7{
    /**
     * 在反射接收一个数组的时候要注意，数组在传入的时候会被拆分
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Method method = clazz.getMethod("main",String[].class);
        //method.invoke(null, new Object[]{new String[]{"1","dede"}});
        method.invoke(null, (Object)new String[]{"1","dede"});
    }
}
class Demo8{
     //反射字段 public String name = "asdf" ;
    public static void main(String[] args) throws Exception{
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Field f = clazz.getField("name");
        //获取字段的值
        Object object = f.get(p);
        //获取字段的类型
        Class type = f.getType();
        if (type.equals(String.class)){
            String name = (String) f.get(p);
            System.out.println(name);
        }
        //设定字段的值
        f.set(p,"重新赋值");
        System.out.println(p.name);
    }
}
class Demo9{
    public static void main(String[] args) throws Exception{
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Field f = clazz.getDeclaredField("password");
        f.setAccessible(true);
        System.out.println(f.get(p));
    }
}
class Demo10{
    public static void main(String[] args) throws Exception{
        Person p = new Person();
        Class clazz = Class.forName("reflect.ReflectDemo1.Person");
        Field f = clazz.getDeclaredField("age");
        f.setAccessible(true);
        System.out.println(f.get(p));
    }
}
